package com.ews.onlineexchange.model.approved;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import com.ews.onlineexchange.model.DataEntity;

@Entity
public class ApprovedMatter extends DataEntity {

	/**
	 * 
	 */
	private static final long			serialVersionUID	= 1L;

	private ApprovedClass				approvedClass;
	private String						matterName;
	private List<EvidenceDefination>	evidenceDefinations	= new ArrayList<EvidenceDefination>();
	private MatterType					matterType;

	@ManyToOne
	@NotNull
	public ApprovedClass getApprovedClass() {
		return approvedClass;
	}

	public void setApprovedClass(ApprovedClass approvedClass) {
		this.approvedClass = approvedClass;
		if (this.approvedClass.getApprovedType() != null && this.approvedClass.getApprovedType().equals(ApprovedType.Violation))
			this.setMatterType(MatterType.Subtract);
	}

	@NotNull
	public String getMatterName() {
		return matterName;
	}

	public void setMatterName(String matterName) {
		this.matterName = matterName;
	}

	@OneToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "ApprovedMatter_DEF")
	public List<EvidenceDefination> getEvidenceDefinations() {
		return evidenceDefinations;
	}

	public void setEvidenceDefinations(List<EvidenceDefination> evidenceDefinations) {
		this.evidenceDefinations = evidenceDefinations;
	}

	@Enumerated(EnumType.STRING)
	@NotNull
	public MatterType getMatterType() {
		return matterType;
	}

	public void setMatterType(MatterType matterType) {
		this.matterType = matterType;
	}
	
	@Transient
	public void addEmptyEvidenceDefination() {
		getEvidenceDefinations().add(new EvidenceDefination());
	}
	
	@Transient
	public void removeEvidenceDefination(int index) {
		getEvidenceDefinations().remove(index);
	}

//	@Transient
//	public String addEmptyLevelOption() {
//		QualificationLevel levelOption = new QualificationLevel();
//		levelOption.setQualificationClass(this);
//		String uuid = SystemUUIDUtil.getUUID();
//		levelOption.setLevelIndex(uuid);
//		getLevelOptions().add(levelOption);
//		return uuid;
//	}
//	
//	@Transient
//	public void removeEvidenceDefination(String index) {
//		List<EvidenceDefination> definations = getEvidenceDefinations();
//		EvidenceDefination entity = null;
//		for (QualificationLevel qualificationLevel : levels) {
//			if(qualificationLevel.getLevelIndex().equals(index)) {
//				removeLevel = qualificationLevel;
//			}
//		}
//		if(removeLevel != null) {
//			levels.remove(removeLevel);
//		}
//	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((approvedClass == null) ? 0 : approvedClass.hashCode());
//		result = prime * result + ((evidenceDefinations == null) ? 0 : evidenceDefinations.hashCode());
		result = prime * result + ((matterName == null) ? 0 : matterName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ApprovedMatter other = (ApprovedMatter) obj;
		if (approvedClass == null) {
			if (other.approvedClass != null)
				return false;
		} else if (!approvedClass.equals(other.approvedClass))
			return false;
//		if (evidenceDefinations == null) {
//			if (other.evidenceDefinations != null)
//				return false;
//		} else if (!evidenceDefinations.equals(other.evidenceDefinations))
//			return false;
		if (matterName == null) {
			if (other.matterName != null)
				return false;
		} else if (!matterName.equals(other.matterName))
			return false;
		return true;
	}

}
